package mk.ukim.finki.studentguide;

import java.util.ArrayList;
import java.util.List;

import mk.ukim.finki.studentguide.xmlconfiguration.Building;
import mk.ukim.finki.studentguide.xmlconfiguration.Level;
import mk.ukim.finki.studentguide.xmlconfiguration.Room;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class ShowBuilding extends Activity {

	private String array_spinner[];
	private int selectedBuilding;
	public List<Level> lstLevels;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_building);
        
        int selectedBuilding = ((MyApplication) this.getApplication()).getSelectedBuilding();
        Building building = ((MyApplication) this.getApplication()).getLstBuildings().get(selectedBuilding);
        lstLevels = building.getLstLevels();
        
        if(((MyApplication) this.getApplication()).isComingFromSearch()){
        	//((MyApplication) this.getApplication()).setComingFromSearch(false);
        	String searchedRoomName = ((MyApplication) getApplication()).getSearchedRoomName();
        	 for(int i=0; i<lstLevels.size();i++){
        		 Level lv = lstLevels.get(i);
        		 List<Room> lstRooms = new ArrayList<Room>();
        		 lstRooms=lv.getLstRooms();
     	        for(int j=0; j<lstRooms.size();j++){
     	        	if(searchedRoomName.equals(lstRooms.get(j).getTitle())){
     	        		 ((MyApplication) this.getApplication()).setSelectedLevel( ((MyApplication) this.getApplication()).getSelectedLevel());
     	        		 ((MyApplication) this.getApplication()).setSelectedLevel(i);
     	        		 Room r = lstRooms.get(j);
     	        		 ((MyApplication) this.getApplication()).setSearchedRoom(r);
     	        	}
     	        }
     	    }
        	 array_spinner=new String[lstLevels.size()];
 		     for(int i=0; i<lstLevels.size();i++){
 		        array_spinner[i]=lstLevels.get(i).getTitle();
 		     }
 		     final Spinner s = (Spinner) findViewById(R.id.spinner1);
	         ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, array_spinner);
	         s.setAdapter(adapter);
	         s.setSelection(((MyApplication) this.getApplication()).getSelectedLevel());
	         s.setEnabled(false);
        	 ShowLevel slu = (ShowLevel) findViewById(R.id.canvas0);
		     slu.setVisibility(View.VISIBLE);
        }else{
	        
		    array_spinner=new String[lstLevels.size()];
		    for(int i=0; i<lstLevels.size();i++){
		        array_spinner[i]=lstLevels.get(i).getTitle();
		    }
	        
		    ((MyApplication) this.getApplication()).setSelectedLevel( ((MyApplication) this.getApplication()).getSelectedLevel());
		    ((MyApplication) this.getApplication()).setLstLevels(lstLevels);
		    
	        final Spinner s = (Spinner) findViewById(R.id.spinner1);
	        ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, array_spinner);
	        s.setAdapter(adapter);
	        s.setSelection(((MyApplication) this.getApplication()).getSelectedLevel());
	        s.setOnItemSelectedListener(new OnItemSelectedListener() {
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					  int selectedPosition = s.getSelectedItemPosition();
					  ShowLevel slu = (ShowLevel) findViewById(R.id.canvas0);
					  ((MyApplication) getApplication()).setSelectedLevel(selectedPosition);
				      slu.setVisibility(View.VISIBLE);
				      if(  ((MyApplication) getApplication()).isCanRefreshActivity()){
	
					     ((MyApplication) getApplication()).setCanRefreshActivity(false);
					      finish();
					      Intent myIntent = new Intent(ShowBuilding.this, ShowBuilding.class);
					      startActivity(myIntent);
				      }
				}
	
				public void onNothingSelected(AdapterView<?> arg0) {
					
				}
	
	        });
	        
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_show_building, menu);
        return true;
    } 
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_settings:
                goToSearchRoom();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    public void goToSearchRoom(){
		  Intent intent=new Intent(this,SearchRoom.class);
	      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	      this.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	((MyApplication) this.getApplication()).setSomeVariable("foo");
        	((MyApplication) this.getApplication()).setSelectedLevel(0);
        	((MyApplication) this.getApplication()).setCanRefreshActivity(false);
            Intent intent = new Intent(this, StudentGuideMapActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
            startActivity(intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    
    public void onStop(){
    	super.onStop();
    	if(!this.isFinishing()){
    		this.finish();
    	}
    }
    
    
	public int getSelectedBuilding() {
		return selectedBuilding;
	}

	public void setSelectedBuilding(int selectedBuilding) {
		this.selectedBuilding = selectedBuilding;
	}

}
